/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.icq.io;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import org.apache.log4j.Logger;

/**
 * TODO НЕ ИСПОЛЬЗУЕТСЯ
 * Загрузка иконок из темы.
 * @author Носов А.В.
 */
public class loadIcons {

    // Variables declaration
    private static final Logger log = Logger.getLogger(loadIcons.class);
    // End of variables declaration

    public static Icon getIcon(String theme, String name) {
        try {
            
            Properties p = new Properties();
            p.load(new FileInputStream(System.getProperty("user.dir") + File.separator +
                "Themes" + File.separator + theme + File.separator + "Icons.properties"));
            
            if (p != null) {
                ImageIcon icon = null;
                String path = p.getProperty(name);
                if (path.endsWith(".png")) {
                    icon = new ImageIcon(System.getProperty("user.dir") + File.separator +
                        "Themes" + File.separator + theme + File.separator +
                        "Icons" + File.separator + path);
                    return icon;
                    
                } else {
                    String[] s = path.split(";");
                    int[] pes = new int[4];
                    for (int i=0; i<s.length; i++)
                        pes[i] = Integer.valueOf(s[i]);
                    
                    File f = new File(System.getProperty("user.dir") + File.separator +
                        "Themes" + File.separator + theme + File.separator +
                        "Icons" + File.separator + p.getProperty(name.substring(0, name.indexOf("."))));
                    
                    BufferedImage im = ImageIO.read(f);
                    ImageFilter filter = new CropImageFilter(pes[0], pes[1], pes[2], pes[3]);
                    ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
                    icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(ip));
                    return icon;
                }
            } return null;
        } catch (IOException ex) {
            log.error("Ошибка чтения файла", ex);
            return null;
        }
    }
}
